package com.treehouse.rpgdefender.system;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.annotations.Mapper;
import com.artemis.systems.EntityProcessingSystem;
import com.treehouse.rpgdefender.component.Sprite;
import com.treehouse.rpgdefender.component.Transformation;

/**
 * System class implements the render logic for all entities with transform and
 * sprite components.
 * 
 * @author Julian Biermann
 * @version 26.02.2013 11:54:50 v1.0
 * 
 */
public class SpriteRenderSystem extends EntityProcessingSystem {

	private GameContainer container;
	private Graphics graphics;
	@Mapper
	private ComponentMapper<Sprite> spriteMapper;
	@Mapper
	private ComponentMapper<Transformation> transformationMapper;

	/**
	 * initializes the Render System, needs the game container for graphic
	 * access.
	 * 
	 * @param container
	 *            Game Container.
	 */
	@SuppressWarnings("unchecked")
	public SpriteRenderSystem(GameContainer container) {
		super(Aspect.getAspectForAll(Transformation.class, Sprite.class));
		this.container = container;
		this.graphics = this.container.getGraphics();
	}

	@Override
	protected void initialize() {
		this.spriteMapper = world.getMapper(Sprite.class);
		this.transformationMapper = world.getMapper(Transformation.class);
	}

	@Override
	protected void process(Entity e) {
		Sprite sprite = this.spriteMapper.get(e);
		Transformation transformation = this.transformationMapper.get(e);
		graphics.drawImage(sprite.getTexture(), transformation.getX(),
				transformation.getY());

	}

	@Override
	protected boolean checkProcessing() {
		return true;
	}
}
